215. 数组中的第K个最大元素
为保证权益,题目请参考 215. 数组中的第K个最大元素(From LeetCode).
解决方案1
CPP
C++
#include<iostream>
#include <vector>
using namespace std;
int getPivot(vector<int> &arr, int startIndex, int endIndex) {
int left = startIndex;
int pivot = arr[left];
int pivotIndex = left;
int right = endIndex;
while (left != right) {
// right to left
while (left < right && arr[right] >= pivot) {
right--;
}
// left to right
while (left < right && arr[left] <= pivot) {
left++;
}
if (left < right) {
int t = arr[left];
arr[left] = arr[right];
arr[right] = t;
}
}
int tmp = arr[left];
arr[left] = pivot;
arr[pivotIndex] = tmp;
return left;
}
void quickSort(vector<int> &arr, int left, int right) {
if (left >= right) {
return;
}
int le = getPivot(arr, left, right);
quickSort(arr, left, le - 1);
quickSort(arr, le + 1, right);
}
class Solution {
public:
int findKthLargest(vector<int> &nums, int k) {
int left = 0;
int right = nums.size() - 1;
k = nums.size() - k;
int ans = findIndex(nums, left, right);
while (ans != k) {
if (ans > k) {
ans = findIndex(nums, left, ans - 1);
} else {
// k = k - ans - 1;
ans = findIndex(nums, ans + 1, right);
}
}
return nums[ans];
}
int findIndex(vector<int> &nums, int left, int right) {
int p = nums[left];
int pi = left;
while (left != right) {
while (left < right && nums[right] >= p) {
right--;
}
while (left < right && nums[left] <= p) {
left++;
}
if (left < right) {
int t = nums[left];
nums[left] = nums[right];
nums[right] = t;
}
}
int t = nums[left];
nums[left] = p;
nums[pi] = t;
return left;
}
};
int main() {
vector<int> arr;
arr.push_back(3);
arr.push_back(2);
arr.push_back(1);
arr.push_back(5);
arr.push_back(6);
arr.push_back(4);
Solution so;
cout << so.findKthLargest(arr, 2) << endl;
for (int i : arr) {
cout << i << endl;
}
return EXIT_SUCCESS;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113